package com.lk.springboot.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lk.springboot.service.HomeSevice;
import com.lk.springboot.service.LoginService;
import com.lk.springboot.vo.MyProps;
import com.lk.springboot.vo.Result;
import com.lk.springboot.vo.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lk
 * @date 2020/3/5 0005 12:01
 * @description: 我的背包（个人主页）模块
 */

@RestController
@RequestMapping("/my")
public class HomeController {

    @Autowired
    LoginService loginService;
    @Autowired
    HomeSevice homeSevice;

    /**
     * 获取道具列表的接口
     *
     * @param token
     * @param start
     * @param size
     * @return
     */
    @RequestMapping(value = "/props", method = RequestMethod.GET)
    public String listProps(@CookieValue("token") String token,
                            @RequestParam(value = "start", defaultValue = "1") int start,
                            @RequestParam(value = "size", defaultValue = "4") int size) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            PageHelper.startPage(start, size);
            List<MyProps> list = homeSevice.findAllMyProps(u_id);
            PageInfo<MyProps> page = new PageInfo<>(list);
            Result result = new Result(ResultCode.SUCCESS);
            result.setData(page);
            return JSONObject.toJSONString(result);
        }
    }

    /**
     * 获取捐赠伞列表的接口
     *
     * @param token
     * @param start
     * @param size
     * @return
     */
    @RequestMapping(value = "/achievement", method = RequestMethod.GET)
    public String listAchivevments(@CookieValue("token") String token,
                                   @RequestParam(value = "start", defaultValue = "1") int start,
                                   @RequestParam(value = "size", defaultValue = "4") int size) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            Page page = PageHelper.startPage(start, size, "date");
            List<JSONObject> list = homeSevice.findAllMyAchievements(u_id);
            PageInfo<JSONObject> pageInfo = new PageInfo<>(list);
            pageInfo.setPages(page.getPages());//总页数
            pageInfo.setTotal(page.getTotal());//总条数
            pageInfo.setPageNum(page.getPageNum());//当前页
            Result result = new Result(ResultCode.SUCCESS);
            result.setData(pageInfo);
            return JSONObject.toJSONString(result);
        }
    }

    /**
     * 获取拥有的拼图列表的接口
     *
     * @param token
     * @param start
     * @param size
     * @return
     */
    @RequestMapping(value = "/picture", method = RequestMethod.GET)
    public String listPictures(@CookieValue("token") String token,
                               @RequestParam(value = "start", defaultValue = "1") int start,
                               @RequestParam(value = "size", defaultValue = "4") int size) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            Page page = PageHelper.startPage(start, size);
            List<JSONObject> list = homeSevice.findAllMyPictures(u_id);
            PageInfo<JSONObject> pageInfo = new PageInfo<>(list);
            pageInfo.setPages(page.getPages());//总页数
            pageInfo.setTotal(page.getTotal());//总条数
            pageInfo.setPageNum(page.getPageNum());//当前页
            Result result = new Result(ResultCode.SUCCESS);
            result.setData(pageInfo);
            return JSONObject.toJSONString(result);
        }
    }

    /**
     * 个人主页信息
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String getMy(@CookieValue("token") String token) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            String result = homeSevice.getMine(u_id);
            return result;
        }

    }

    /**
     * 点赞
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "like", method = RequestMethod.GET)
    public String addLike(@CookieValue("token") String token) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            String result = homeSevice.addLikes(u_id);
            return result;
        }

    }

    /**
     * 用户使用了道具
     *
     * @param token
     * @param props_id
     * @return
     */
    @RequestMapping(value = "useprops", method = RequestMethod.POST)
    public String useProps(@CookieValue("token") String token,
                           @RequestParam(value = "props_id") int props_id) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            String result = homeSevice.reducePropsNum(u_id, props_id);
            return result;
        }

    }
}
